import { createRouter, createWebHistory } from 'vue-router'
// 获取token
import { thisPinia } from '@/pinia/index';

const routes = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login.vue')
    },
    {
        path: '/xxx',
        name: 'loxxgin',
        component: () => import('@/views/modulex.vue')
    },
    {
        path: '/',
        name: 'homepage',
        component: () => import('@/views/homepage.vue')
    },
    {
        path: '/study/record',
        name: 'study-record',
        component: () => import('@/views/study/record.vue')
    },
    {
        path: '/study/search',
        name: 'study-search',
        component: () => import('@/views/study/search.vue')
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: () => import('@/views/notFound.vue') }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    const { token } = thisPinia()
    if (to.path == '/login' && token) {
        next('/');
    } else if (to.path !== '/login' && !token) {
        next('/login');
    } else {
        next();
    }
});

export default router;